﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using G.Model;

namespace GameControls
{
    public class BoardShapeContainer : List<BoardShape>
    {
        private readonly GameUserControl _GameUserControl;
        private readonly int _SquareSize;
        
        private BoardShape _HeighlightedShape;
        private ActiveObject _ActiveObject;
        
        public BoardShapeContainer(GameUserControl pGameUserControl, 
                                   int pSquareSize)
        {
            _GameUserControl = pGameUserControl;
            _SquareSize = pSquareSize;
        }
        
        public Point ToPoint(GameLocation loc)
        {
            return new Point(loc.X * _SquareSize, loc.Y * _SquareSize);
        }

        public GameLocation ToGameLocation(Point loc)
        {
            return new GameLocation(loc.X / _SquareSize, loc.Y / _SquareSize);
        }
        
        public int SquareSize
        {
            get { return _SquareSize; }
        }
        
        public void OnMouseClick(object sender, MouseEventArgs e) 
        {
            foreach (BoardShape shape in this) 
            {
                if (shape.HitTest(e.Location))
                {
                    shape.OnMouseClick(this, e);
                    break;
                }
            }
        }
        
        public void OnPaint(object sender, PaintEventArgs e)
        {
            foreach (BoardShape shape in this) {
                shape.OnPaint(this, e);
            }
        }
        
        public BoardShape HeighlightedShape
        {
            get { return _HeighlightedShape; }
            set 
            { 
                BoardShape save = _HeighlightedShape;
                _HeighlightedShape = value; 
                if (save != null)
                {
                    save.Invalidate();
                }
                if (_HeighlightedShape != null)
                {
                    _HeighlightedShape.Invalidate();
                }
            }
        }
        
        public GameUserControl GameUserControl
        {
            get { return _GameUserControl; }
        }
        
        public ActiveObject ActiveObject
        {
            get { return _ActiveObject; }
            set 
            { 
                if (_ActiveObject != null)
                {
                    _ActiveObject.SelectedEvent -= OnSelectedEvent;
                }
                _ActiveObject = value; 
                _ActiveObject.SelectedEvent += OnSelectedEvent;
            }
        }
        
        private void OnSelectedEvent(object obj)
        {
//            if (this.Contains(obj))
//            {
//                BoardShape shape = (BoardShape)obj;
//                _HeighlightedShape = shape;
//            }
        }
    }
}
